package net.ojbk.study.twobox.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.ojbk.study.twobox.beans.po.Refer;
import net.ojbk.study.twobox.beans.po.Student;
import net.ojbk.study.twobox.beans.vo.PayStudent;
import net.ojbk.study.twobox.beans.vo.SelectOption;
import net.ojbk.study.twobox.dao.ClassMapper;
import net.ojbk.study.twobox.dao.ReferMapper;
import net.ojbk.study.twobox.dao.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;

import java.lang.reflect.Field;
import java.util.List;
import java.util.UUID;

@Service
public class O_PayStudentService {

    @Autowired
    ReferMapper referMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    StudentService studentService;

    // 属性验证异常处理
    public void validated(BindingResult br, Class aClass) throws Exception {
        List<ObjectError> errors = br.getAllErrors();
        if (errors.size() > 0) {
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                FieldError fieldError = br.getFieldError(field.getName());
                if (fieldError != null)
                    throw new Exception(fieldError.getDefaultMessage());
            }
        }
    }

    public PageInfo<PayStudent> getAll_PayStudentList(int currentPage) throws Exception{
        PageHelper.startPage(currentPage, 15);
        List<PayStudent> payStudentList = referMapper.getAll_PayStudentList();
        PageInfo<PayStudent> pageInfo = new PageInfo<>(payStudentList);
        return pageInfo;
    }

    public List<SelectOption> getAllClass() {
        return referMapper.getAllClass();
    }

    public void referTOStudent(PayStudent payStudents, BindingResult br) throws Exception {
        validated(br, PayStudent.class);
        // 获得这个Refer对象
        Refer refer = referMapper.selectByPrimaryKey(payStudents.getId());
        if (refer == null)
            throw new Exception("此付费了的咨询对象不存在。");
        // 设置这个对象为结束状态
        refer.setRstate("结束");
        referMapper.updateByPrimaryKey(refer);
        // 产生一个新的学生
        Student student = new Student();
        student.setStuname(refer.getUsername());
        student.setStucode(UUID.randomUUID().toString());
        student.setStusex(refer.getGender());
        student.setReferid(refer.getId());
        student.setClassid(payStudents.getClassid());
        student.setBegintime(payStudents.getBegintime());
        student.setJobtime(payStudents.getJobtime());
        student.setStustate("学习中");
        student.setChecklevel("");
        student.setRemark("");
        studentService.check(student);
        // 存入数据库
        studentMapper.insert(student);
    }
}





















